import { createRouter, createWebHistory } from 'vue-router'
import { defineAsyncComponent } from 'vue'




const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            name: 'Entrance',
            component: defineAsyncComponent(() => import('@/views/ContainerIndex.vue')),
            redirect: '/recomm',
            children: [
                {
                    path: '/home',
                    name: 'Home',
                    component: defineAsyncComponent(() => import('@/views/client/home/home-index.vue'))
                },
                {
                    path: "/order",
                    name: "Order",
                    component: defineAsyncComponent(() => import('@/views/client/order/OrderPage.vue')),
                    meta: { isLogin: true, grade: 'CLIENT' }
                },
                {
                    path: "/recomm",
                    name: "Recomm",
                    component: defineAsyncComponent(() => import('@/views/client/home/RecommVideo.vue'))
                },
                {
                    path: "/center",
                    name: "Center",
                    component: defineAsyncComponent(() => import('@/views/client/user/PersonCenter.vue')),
                    meta: { isLogin: true, grade: 'CLIENT' }
                },
                {
                    path: "/client/search",
                    name: "Search",
                    component: defineAsyncComponent(() => import('@/views/client/search/SearchPage.vue')),
                },
                {
                    path: "/client/hit",
                    name: "Hit",
                    component: defineAsyncComponent(() => import('@/views/client/home/HitVideo.vue')),
                },
                {
                    path: "/client/concern",
                    name: "Concern",
                    component: defineAsyncComponent(() => import('@/views/client/home/ConcernVideo.vue')),
                },
                {
                    path: "/client/userinfo",
                    name: "UserInfo",
                    component: defineAsyncComponent(() => import('@/views/client/user/UserInfo.vue')),
                    meta: { isLogin: true, grade: 'CLIENT' }
                },
                {
                    path: "/client/detail",
                    name: "Detail",
                    component: defineAsyncComponent(() => import('@/views/client/video/VideoDetail.vue'))
                },
                {
                    path: '/client/classify',
                    name: "Classify",
                    component: defineAsyncComponent(() => import('@/views/client/classify/ClassifyIndex.vue')),
                },
            ]
        },
        {
            path: "/client/setting",
            name: "UserSetting",
            component: defineAsyncComponent(() => import('@/views/client/user/UserSetting.vue')),
            meta: { isLogin: true, grade: 'CLIENT' }
        },

        {
            path: "/client/history/video",
            name: "HistoryVideo",
            component: defineAsyncComponent(() => import('@/views/client/user/VideoHistory.vue')),
            meta: { isLogin: true, grade: 'CLIENT' }
        },
        {
            path: "/client/history/buy",
            name: "HistoryBuy",
            component: defineAsyncComponent(() => import('@/views/client/user/BuyHistory.vue')),
            meta: { isLogin: true, grade: 'CLIENT' }
        },
        {
            path: "/client/buy/gold",
            name: "BuyGold",
            component: defineAsyncComponent(() => import('@/views/client/user/BuyGold.vue')),
            meta: { isLogin: true, grade: 'CLIENT' }
        },
        {
            path: "/client/history/recharge",
            name: "Recharge",
            component: defineAsyncComponent(() => import('@/views/client/user/RechargeHistory.vue')),
            meta: { isLogin: true, grade: 'CLIENT' }
        },
        {
            path: "/client/video/publish",
            name: "Publish",
            component: defineAsyncComponent(() => import('@/views/client/user/VideoPublish.vue')),
            meta: { isLogin: true, grade: 'CLIENT' }
        },
        {
            path: '/admin',
            name: 'Admin',
            component: defineAsyncComponent(() => import('@/views/admin/AdminHome.vue')),
            redirect: '/admin/home',
            meta: { isLogin: true, grade: 'ADMIN' },
            children: [
                {
                    path: '/admin/home',
                    name: "AdminHome",
                    component: defineAsyncComponent(() => import('@/views/admin/HomePage.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/common/setting',
                    name: "CommonSetting",
                    component: defineAsyncComponent(() => import('@/views/admin/setting/CommonSetting.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/notice',
                    name: "Notice",
                    component: defineAsyncComponent(() => import('@/views/admin/notice/AdminNotice.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                // {
                //     path: '/admin/permissions',
                //     name: "Permissions",
                //     component: defineAsyncComponent(() => import('@/views/admin/permissions/AdminPermissions.vue'))
                // },
                {
                    path: '/admin/permissions/role',
                    name: "Roles",
                    component: defineAsyncComponent(() => import('@/views/admin/permissions/AdminRoles.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/user/list',
                    name: "UserList",
                    component: defineAsyncComponent(() => import('@/views/admin/user/AdminUser.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/user/vip',
                    name: "UserVip",
                    component: defineAsyncComponent(() => import('@/views/admin/user/AdminVip.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/loginfor',
                    name: 'LogInfor',
                    component: () => import('@/views/admin/log/SysLogininfor.vue'),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/fast',
                    name: 'Faster',
                    component: () => import('@/views/admin/setting/FasterSetting.vue'),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/type',
                    name: "Type",
                    component: defineAsyncComponent(() => import('@/views/admin/type/AdminType.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/test',
                    name: 'Test',
                    component: () => import('@/views/admin/log/SysLogininfor.vue'),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/pay',
                    name: "Pay",
                    component: defineAsyncComponent(() => import('@/views/admin/setting/PaySetting.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/shop',
                    name: "Shop",
                    component: defineAsyncComponent(() => import('@/views/admin/shop/ShopList.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/order',
                    name: "Order",
                    component: defineAsyncComponent(() => import('@/views/admin/shop/OrderList.vue')),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/operlog',
                    name: 'OperLog',
                    component: () => import('@/views/admin/log/SysOperLog.vue'),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {
                    path: '/admin/maillog',
                    name: 'MailLog',
                    component: () => import('@/views/admin/log/MailLog.vue'),
                    meta: { isLogin: true, grade: 'ADMIN' }
                },
                {

                    path: '/admin/videomg',
                    name: 'VideoMg',
                    component: () => import('@/views/admin/video/VideoMg.vue')
                },
                {
                    path: '/admin/videoreview',
                    ame: 'VideoReview',
                    component: () => import('@/views/admin/video/VideoReview.vue')
                },
                {
                    path: '/admin/videoadverties',
                    name: 'VideoAdverties',
                    component: () => import('@/views/admin/video/VideoAdvertise.vue')
                },

            ]
        },
        {
            path: '/alogin',
            name: 'AdminLogin',
            component: defineAsyncComponent(() => import('@/views/login/aLogin.vue')),
        },
        {
            path: '/login',
            name: 'UserLogin',
            component: defineAsyncComponent(() => import('@/views/login/uLogin.vue')),
        },
        {
            path: '/error',
            name: 'Error',
            component: defineAsyncComponent(() => import('@/views/error/NotFound.vue'))
        },
        {
            path: '/register',
            name: 'Register',
            component: defineAsyncComponent(() => import('@/views/login/uRegister.vue'))
        },
    ]
})

// 检查是否存在这个路由
// function checkRouteExists(route, to) {
//     if (route.path === to.path) {
//         return true;
//     }
//     if (route.children) {
//         for (let i = 0; i < route.children.length; i++) {
//             if (checkRouteExists(route.children[i], to)) {
//                 return true;
//             }
//         }
//     }
//     return false;
// }

router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    console.log(token)
    var user = localStorage.getItem('user')

    if (user != null) {
        user = JSON.parse(user)
    }

    // const exist = router.getRoutes().some(route => checkRouteExists(route, to));
    // if (!exist) {
    //     next('/error');
    // } else {
    //     next();
    // }

    if (to.name === 'Login') {
        next()
        return
    }
    console.log(user)
    if (to.meta.isLogin) {
        // 判断是否需要登录
        if (token != null && user != null) {
            if (to.meta.grade === "ADMIN" && user.issuer === "ADMIN") {
                // 判断等级
                next()
                return
            } else if (to.meta.grade === "CLIENT" && user.issuer === "CLIENT") {
                next()
                return
            } else {
                router.push('/login')
                return
            }
        } else {
            router.push('/login')
            return
        }
    }
    next()
})

export default router
